package com.wzq.app.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;

/**
 */

public abstract class BaseFragment extends Fragment {
    private boolean isPrepared;
    private boolean isVisible;
    private boolean isLoaded;
    public Context mContext;
    public View mView;
    private Unbinder unbinder;
    private List<Call> calls = new ArrayList<>();
    private MaterialDialog materialDialog;

    /**
     * 该fragment所在的Activity被启动所回调的方法
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getUserVisibleHint();
    }


    protected void onVisible() {
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
        }
    }

    public boolean isUseEventBus() {
        return false;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDetach() {
        if (isUseEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        super.onDetach();
    }

    /**
     * 初始化控件资源
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        configView();
        isPrepared = true;
        onVisible();
    }

    /**
     * 系统会在Fragment首次绘制其用户界面时调用此方法。
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, mView);
        return mView;
    }


    public abstract @LayoutRes
    @NonNull
    int getLayoutId();

    private void lazyLoad() {
        if (!isPrepared || !isVisible || isLoaded) {
            return;
        }
        requestData();
        isLoaded = true;
    }

    public abstract void configView();


    /**
     * 懒加载,页面可见时才会被调用
     */
    public abstract void requestData();

    @Override
    public void onDestroy() {
        if(unbinder!=null) {
            unbinder.unbind();
        }
        cancel();
        super.onDestroy();
    }

    public void cancel() {
        for (Call call : calls) {
            if (call != null && !call.isCanceled()) {
                call.cancel();
            }
        }
    }

    public void putCall(Call... datas) {
        for (Call call : datas) {
            if (call != null && !calls.contains(call)) {
                calls.add(call);
            }
        }
    }

    public void showProgressDialog(){
        showProgressDialog("加载中...");
    }
    public void showProgressDialog(String content){
        if(materialDialog == null){
            materialDialog = new MaterialDialog.Builder(mContext)
                    .content(content)
                    .cancelable(false)
                    .progress(true, 0)
                    .show();
        }
    }
    public void dimissProgressDialog(){
        if(materialDialog!=null&&materialDialog.isShowing()){
            materialDialog.dismiss();
            materialDialog = null;
        }
    }
}
